package com.gdut.simple.view.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.gdut.simple.R;
import com.gdut.simple.common.LogUtils;
import com.gdut.simple.common.UserUtils;
import com.gdut.simple.data.entity.User;
import com.gdut.simple.event.RxBus;
import com.gdut.simple.event.UserEvent;
import com.gdut.simple.view.activity.user.AboutActivity;
import com.gdut.simple.view.activity.user.CommentActivity;
import com.gdut.simple.view.activity.user.FavoritesActivity;
import com.gdut.simple.view.activity.user.HistoryActivity;
import com.gdut.simple.view.activity.user.LoginActivity;
import com.gdut.simple.view.activity.user.FollowActivity;
import com.gdut.simple.view.activity.user.PostActivity;
import com.gdut.simple.view.activity.user.PreferenceActivity;
import com.gdut.simple.view.activity.user.UserPersonalInfoActivity;
import com.squareup.picasso.Picasso;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobUser;
import de.hdodenhof.circleimageview.CircleImageView;
import rx.Subscription;
import rx.functions.Action1;

public class UserFragment extends Fragment {

    private static final String TAG = UserFragment.class.getSimpleName();

    @BindView(R.id.id_toolbar)
    Toolbar mToolbar;
    @BindView(R.id.id_user_name_text)
    TextView mUserNameText;
    @BindView(R.id.id_follow_user_profile)
    CircleImageView mUserProfile;

    @OnClick(R.id.id_my_fav_button)
    void gotoFavourites() {
        startActivity(new Intent(getActivity(), FavoritesActivity.class));
    }

    @OnClick(R.id.id_user_config_layout)
    void gotoConfig() {
        if (UserUtils.isLogin()) {
            startActivity(new Intent(getActivity(), UserPersonalInfoActivity.class));
        } else {
            startActivity(new Intent(getActivity(), LoginActivity.class));
        }
    }

    @OnClick(R.id.id_my_follow_button)
    void gotoFollow() {
        startActivity(new Intent(getActivity(), FollowActivity.class));
    }

    @OnClick(R.id.id_my_post_button)
    void gotoPost() {
        startActivity(new Intent(getActivity(), PostActivity.class));
    }

    @OnClick(R.id.id_my_comment_button)
    void gotoComment() {
        startActivity(new Intent(getActivity(), CommentActivity.class));
    }

    @OnClick(R.id.id_my_history_button)
    void gotoHistory() {
        startActivity(new Intent(getActivity(), HistoryActivity.class));
    }

    @OnClick(R.id.id_pref_button)
    void gotoPref() {
        startActivity(new Intent(getActivity(), PreferenceActivity.class));
    }

    @OnClick(R.id.id_about_button)
    void gotoAbout() {
        startActivity(new Intent(getActivity(), AboutActivity.class));
    }

    private Subscription mSubscription;

    public static UserFragment newInstance() {
        return new UserFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user, container, false);
        ButterKnife.bind(this, view);
        mToolbar.setTitle(R.string.nav_user);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        updateUserConfig();
        mSubscription = RxBus.getDefault().toObservable(UserEvent.class).subscribe(new Action1<UserEvent>() {
            @Override
            public void call(UserEvent event) {
                updateUserConfig();
            }
        });
    }

    void updateUserConfig() {
        if (!UserUtils.isLogin()) {
            LogUtils.e(TAG, "not login");
            mUserNameText.setText("未登陆，点击此处登陆");
            mUserProfile.setImageResource(R.drawable.ic_profile);
        } else {
            User user = BmobUser.getCurrentUser(User.class);
            mUserNameText.setText(user.getNickName());
            if (user.getProfile() != null) {
                Picasso.with(getContext())
                        .load(user.getProfile().getUrl())
                        .error(R.drawable.ic_load_error)
                        .into(mUserProfile);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mSubscription != null && mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }

}
